package com.ahsjdy.stcommon.util;


import com.ahsjdy.stcommon.dto.LonLatDTO;

/**
 * 计算两个坐标经纬度直线距离
 */
public class CalculateLonLatUtils {

    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }


    /**
     * 目前使用此方法计算两点距离
     * 单位 公里
     * @param oneLon
     * @param oneLat
     * @param anOtherLon
     * @param anOtherLat
     * @return
     */
    public static double distance(double oneLon, double oneLat, double anOtherLon, double anOtherLat) {
        double radLat1 = rad(oneLat);
        double radLat2 = rad(anOtherLat);
        double a = radLat1 - radLat2;
        double b = rad(oneLon) - rad(anOtherLon);
        double distanceValue = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        distanceValue = distanceValue * EARTH_RADIUS;
        distanceValue = Math.round(distanceValue * 10000d) / 10000d;
        return distanceValue;
    }

    public static void main(String[] args) {

        LonLatDTO one = new LonLatDTO("120.2158101012", "30.8819515287");
        LonLatDTO anOther = new LonLatDTO("120.2778259280", "30.8851711977");

//        LonLatDTO one = new LonLatDTO("118.7020846198", "32.1607032396");
//        LonLatDTO anOther = new LonLatDTO("118.7035641839", "32.1689156967");//0.9236980682044494

    }
}
